package org.anupta.realm.server.realm.packet.account;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.anupta.realm.entity.entities.Player;
import org.anupta.realm.main.Main;
import org.anupta.realm.server.realm.client.RealmClient;
import org.anupta.realm.server.realm.packet.RealmPacket;

public class ServerList extends RealmPacket {

	public ServerList(RealmClient client) {
		super(client);
		pack();
		doPacket();
	}

	@Override
	public void unpack() {
	}

	@Override
	public void pack() {
		packetOut.append("AxK")
		.append("31536000000");//TODO: Suscribe time
		Map<Integer,Byte> data = new TreeMap<Integer,Byte>();
		for (Player player: client.getAccount().getPlayers().values()) {
			int gameGuid = player.getGameGuid();
			if(!data.containsKey(gameGuid)){
				data.put(gameGuid,(byte) 1);
			}else{
				int count = data.get(gameGuid);
				data.remove(gameGuid);
				data.put(gameGuid,(byte)(count+1));
			}
        }
		for(Entry<Integer,Byte> entry : data.entrySet()){
			packetOut.append("|").append(entry.getKey()).append(",").append(entry.getValue());
		}
	}

	@Override
	public void doPacket() {
		client.send(packetOut.toString());
		Main.out.println(new StringBuilder("ServerList packet sended to : ").append(client.getIp()).toString());
	}
}
